﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using PInitiatives.Models;
using System.Net;
using PInitiatives.Helpers;

namespace PInitiatives.Outlook
{
    public class OutlookMeetingGenerator
    {
        public static void SendICalendarEmail(DateTime start, DateTime end, int attendeeID, string attendeeName, string attendeeEmail, int initiativeID, string organizerUserName, string subject, string description, string location)
        {
            System.Text.StringBuilder sw = new System.Text.StringBuilder();

            //uid 
            Guid UID = Guid.NewGuid();

            sw.AppendLine("BEGIN:VCALENDAR");
            sw.AppendLine("VERSION:2.0");
            sw.AppendLine("METHOD:REQUEST");
            sw.AppendLine("BEGIN:VEVENT");
            sw.AppendLine("ATTENDEE;CN=\"" + attendeeName + "(" + attendeeEmail + ")\";RSVP=TRUE:mailto:" + attendeeEmail);
            sw.AppendLine("CLASS:PUBLIC");
            sw.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
            sw.AppendLine("DESCRIPTION:" + description);
            sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", end));
            sw.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
            sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", start));
            sw.AppendLine("ORGANIZER;CN=\"NAME\":mailto:" + organizerUserName);
            sw.AppendLine("SEQUENCE:0");
            sw.AppendLine("UID:" + UID.ToString());
            sw.AppendLine("LOCATION:" + location);
            sw.AppendLine("SUMMARY;LANGUAGE=en-us:" + subject);
            sw.AppendLine("BEGIN:VALARM");
            sw.AppendLine("TRIGGER:-PT720M");
            sw.AppendLine("ACTION:DISPLAY");
            sw.AppendLine("DESCRIPTION:Reminder");
            sw.AppendLine("END:VALARM");
            sw.AppendLine("END:VEVENT");
            sw.AppendLine("END:VCALENDAR");

            

            String meetingInfo = sw.ToString();
            System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType("text/calendar; method=REQUEST");
            AlternateView ICSview = AlternateView.CreateAlternateViewFromString(meetingInfo, mimeType);

            string senderAddress = SettingsManager.GetSettingValue("Outlook.Email.Address");
            string senderPassword = SettingsManager.GetSettingValue("Outlook.Email.Password");
            string recieverAddress = attendeeEmail;

            
            var fromAddress = new MailAddress(senderAddress, "From " + senderAddress);
            var toAddress = new MailAddress(recieverAddress, "To " + recieverAddress);
            string fromPassword = senderPassword;

            //create email message with alternative view containing callendar request
            MailMessage message = new MailMessage(fromAddress, toAddress);
            message.AlternateViews.Add(ICSview);

            //connect to gmail smtp sever
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            //send the message
            try
            {
                PInitiativesEntities db = new PInitiativesEntities();
                smtp.Send(message);
                AcceptanceList aList = new AcceptanceList(UID, attendeeID, "Outlook", initiativeID.ToString());
                db.AcceptanceLists.Add(aList);
                db.SaveChanges();
                
                //PInitiativesEntities db = new PInitiativesEntities();
                //InitiativeUser initativeUser = new InitiativeUser();
                //initativeUser.InitiativeID = initiativeID;
                //initativeUser.UserID = attendeeID;
                //initativeUser.InvitationID = UID;
                //initativeUser.State = "WaitingForAccept";
                ////initativeUser.IsDecided = false;
                //db.InitiativeUsers.Add(initativeUser);
                //db.SaveChanges();

                //Label1.Text = "Wiasomosc wyslana pomyslnie! do: " + recieverAddress;
            }
            catch (Exception ex)
            {
                //Label1.Text = ex.ToString();
            }
        }

        
    }

   
}